package whf.framework.web.tag;

import javax.servlet.jsp.JspException;

import whf.framework.datasource.DataRow;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.entity.IProperty;
import whf.framework.meta.entity.IWriteonlyProperty;
import whf.framework.util.BeanUtils;
import whf.framework.util.StringUtils;
import whf.framework.web.tag.ext.TagStructureException;

/**
 * @author wanghaifeng
 */
public class TableColumnTag extends HtmlTag implements IWriteonlyProperty {
	private final static long serialVersionUID = 0;

	private static Log log = LogFactory.getLog(TableTag.class);

	private String format; // 锟斤拷示锟斤拷式锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟街碉拷使锟斤拷

	private String property; // 锟斤拷示锟斤拷锟皆ｏ拷锟斤拷锟斤拷锟角憋拷锟绞?

	private int maxLength; // 锟斤拷示锟斤拷蟪ざ龋锟斤拷锟斤拷锟绞憋拷锟斤拷囟锟?
	
	private int displayWidth;
	
	private boolean required;
	
	private String maxValue;
	
	private String minValue;
	
	private String queryString;

	private boolean sortable;
	
	private String dictCategoryCode;
	
	private boolean allowMultiRows;
	
	private String htmlAttributes;
	
	private String hyperLink;
	
	private String jsFunction;
	
	private int visibleLevel;
	
	private int editableLevel;
	
	private int colspan;
	
	private boolean enumSelections;
	
	private boolean readonly;
	
	private String dataType;
	
	private String displayTitle;
	
	private String displayType;
	
	private boolean visible = true;
	
	private boolean viewObjectDetail;
	
	private boolean queryCondition;
	
	private String remarks;
	
	private String regexPattern;
	
	private String regexPatternSample;
	
	/**
	 * 直锟斤拷锟斤拷锟饺笔★拷锟较低筹拷锟斤拷腔峋拷锟揭幌碉拷械锟斤拷卸锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟街憋拷锟斤拷锟斤拷
	 * 锟斤拷锟皆达拷锟斤拷使锟斤拷锟斤拷tablecolumn锟叫憋拷痰锟斤拷锟斤拷
	 * @property boolean:outputDirect
	 */
	private boolean outputDirect;
	
	private TableColumn column;

	public void setFormat(String format) {
		this.format = format;
	}

	public void setSortable(boolean jsSort) {
		this.sortable = jsSort;
	}

	public void setMaxLength(int length) {
		this.maxLength = length;
	}

	public void setProperty(String property) {
		this.property = property;
	}
	
	public void setMaxValue(String maxValue) {
		this.maxValue = maxValue;
	}

	public void setMinValue(String minValue) {
		this.minValue = minValue;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public void setQueryString(String query){
		this.queryString = query;
	}
	
	public void setDisplayWidth(int width){
		this.displayWidth = width;
	}
	
	public void setAllowMultiRows(boolean allowMultiRows) {
		this.allowMultiRows = allowMultiRows;
	}

	public void setDictCategoryCode(String dictCategoryCode) {
		this.dictCategoryCode = dictCategoryCode;
	}

	public void setHyperLink(String hyperLink) {
		this.hyperLink = hyperLink;
	}

	public final void setHtmlAttributes(String htmlAttributes) {
		this.htmlAttributes = htmlAttributes;
	}

	public void setJsFunction(String jsFunction) {
		this.jsFunction = jsFunction;
	}

	public void setVisibleLevel(int visibleLevel) {
		this.visibleLevel = visibleLevel;
	}

	public void setEditableLevel(int editableLevel){
		this.editableLevel = editableLevel;
	}
	
	/**
	 * @param enumSelections The enumSelections to set.
	 */
	public final void setEnumSelections(boolean enumSelections) {
		this.enumSelections = enumSelections;
	}
	
	/**
	 * @param readonly The readonly to set.
	 */
	public final void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	/**
	 * @param dataType The dataType to set.
	 */
	public final void setDataType(String dataType) {
		this.dataType = dataType;
	}

	/**
	 * @param viewObjectDetail The viewObjectDetail to set.
	 */
	public void setViewObjectDetail(boolean viewObjectDetail) {
		this.viewObjectDetail = viewObjectDetail;
	}

	/**
	 * @param displayType The displayType to set.
	 */
	public void setDisplayType(String displayType) {
		this.displayType = displayType;
	}

	/**
	 * @param displayTitle The displayTitle to set.
	 */
	public void setDisplayTitle(String displayTitle) {
		this.displayTitle = displayTitle;
	}

	/**
	 * @param queryCondition The queryCondition to set.
	 */
	public void setQueryCondition(boolean queryCondition) {
		this.queryCondition = queryCondition;
	}

	/**
	 * @param colspan The colspan to set.
	 */
	public void setColspan(int colspan) {
		this.colspan = colspan;
	}

	/**
	 * @param visible The visible to set.
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	/**
	 * @param remarks The remarks to set.
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	/**
	 * @param outputDirect The outputDirect to set.
	 */
	public final void setOutputDirect(boolean outputDirect) {
		this.outputDirect = outputDirect;
	}

	/**
	 * @param regexPattern The regexPattern to set.
	 */
	public void setRegexPattern(String regexPattern) {
		this.regexPattern = regexPattern;
	}

	/**
	 * @param regexPatternSample The regexPatternSample to set.
	 */
	public void setRegexPatternSample(String regexPatternSample) {
		this.regexPatternSample = regexPatternSample;
	}

	//
	private TableTag getTableTag() {
		return (TableTag) findAncestorWithClass(this, TableTag.class);
	}
	
	public int doStartTag() throws JspException {
		TableTag tableTag = this.getTableTag();
		if (tableTag == null) {
			log.error(this);
			throw new TagStructureException(getClass(), "");
		}
		if (tableTag.isFirstIteration()) {
			IProperty propertyMeta = null;
			if (tableTag.getBoMeta() != null)
				propertyMeta = tableTag.getBoMeta().findProperty(this.property);
			if (propertyMeta != null) {
				this.column = new TableColumn(propertyMeta);
			} else {
				this.column = new TableColumn(this.property);
			}
			this.column.setFormat(this.format);
			this.column.setJsSort(this.sortable);
			this.column.setMaxLength(this.maxLength);
			this.column.setProperty(this.property);
			this.column.setDataType(this.dataType);
			this.column.setHtmlAttributes(this.htmlAttributes);
			this.column.setRequired(this.required);
			this.column.setMaxValue(this.maxValue);
			this.column.setMinValue(this.minValue);
			this.column.setQueryString(this.queryString);
			this.column.setDisplayWidth(this.displayWidth);
			this.column.setColspan(this.colspan);
			this.column.setAllowMultiRows(this.allowMultiRows);
			this.column.setDictCategoryCode(this.dictCategoryCode);
			this.column.setHyperLink(this.hyperLink);
			this.column.setJsFunction(this.jsFunction);
			this.column.setVisibleLevel(this.visibleLevel);
			this.column.setEditableLevel(this.editableLevel);
			this.column.setEnumSelections(this.enumSelections);
			this.column.setReadonly(this.readonly);
			this.column.setOutputDirect(this.outputDirect);
			if(!StringUtils.isEmpty(this.displayTitle)){
				this.column.setDisplayTitle(this.displayTitle);
			}else{
				this.column.setDisplayTitle(this.property);
			}
			this.column.setDisplayType(this.displayType);
			this.column.setVisible(this.visible);
			this.column.setQueryCondition(this.queryCondition);
			this.column.setViewObjectDetail(this.viewObjectDetail);
			this.column.setRemarks(this.remarks);
			this.column.setRegexPattern(this.regexPattern);
			this.column.setRegexPatternSample(this.regexPatternSample);
			tableTag.getTableModel().addColumn(this.column);
		}
		return 2;
	}

	public int doEndTag() throws JspException {
		TableTag tableTag = this.getTableTag();
		if (tableTag == null) {
			log.error(this);
			throw new TagStructureException(getClass(), "");
		}
		if(tableTag.getCurrentRow() != null){
			DataRow rowObject = tableTag.getCurrentRow().getRowObject();
			String text = "";
			if(this.getBodyContent() != null && !StringUtils.isEmpty(this.getBodyContent().getString())){
				text = this.getBodyContent().getString();
				if(text != null){
					text = BeanUtils.replaceByProperty(rowObject, text);
				}
				if(StringUtils.isEmpty(text)) text = "&nbsp;";
				tableTag.getCurrentRow().setPropertyValue(this.property, text);
			}
		}
		this.release();
		return SKIP_BODY;
	}
	
	public void release(){
		this.allowMultiRows = false;
		this.colspan = 1;
		this.column = null;
		this.dataType = null;
		this.dictCategoryCode = null;
		this.displayWidth = 0;
		this.enumSelections = false;
		this.format = null;
		this.htmlAttributes = null;
		this.hyperLink = null;
		this.jsFunction = null;
		this.displayTitle = null;
		this.maxLength = 0;
		this.maxValue = null;
		this.minValue = null;
		this.outputDirect = false;
		this.property = null;
		this.queryString = null;
		this.readonly = false;
		this.required = false;
		this.sortable = false;
		this.visibleLevel = 0;
		this.editableLevel = 0;
		this.displayType = null;
		this.visible = true;
		this.queryCondition = false;
		this.viewObjectDetail = false;
		this.remarks = null;
		this.regexPattern = null;
		super.release();
	}
}
